package token

import (
	"chatgpt/helper/code"
	"chatgpt/logq"
	"chatgpt/model/gptm"
	dbcounter "chatgpt/services/datasrv/counter"
	"chatgpt/services/datasrv/sqlhelper"
	"context"
)

type TokensSrv struct {
}

func (self *TokensSrv) GetListDB() []*gptm.Tokens {
	datapp := dbcounter.CounterApi{}
	res := sqlhelper.ResWhere{}
	mc := datapp.TokensSelect(context.Background(), &sqlhelper.ReqWhere{}, &res)
	if res.MsgCode != code.SUCCESS {
		logq.InfoF("BenefitUser.GET code:%v", mc)
		return nil
	}
	data_arr, ok := res.Data.([]any)
	if !ok {
		return nil
	}

	rtn := []*gptm.Tokens{}
	for _, v := range data_arr {
		m, ok := v.(map[string]any)
		if !ok {
			continue
		}
		tmp := &gptm.Tokens{}
		tmp.ConvFromMap(m)
		rtn = append(rtn, tmp)
	}
	return rtn
}

func (self *TokensSrv) Save(token *gptm.Tokens) code.CODENO {
	datapp := dbcounter.CounterApi{}
	res := sqlhelper.ResWhere{}
	mc := datapp.TokensSave(context.Background(), &sqlhelper.ReqWhere{
		Where: &sqlhelper.Where{
			Conds: []*sqlhelper.CondItem{
				sqlhelper.WhereCond("TokensID", token.TokensID),
			},
			Formula: "{1}",
		},
	}, &res)
	if res.MsgCode != code.SUCCESS {
		logq.InfoF("BenefitUser.GET code:%v", mc)
		return res.MsgCode
	}
	return code.SUCCESS
}
